﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace weighing
{
    /// <summary>
    /// 过磅设置控件 的交互逻辑
    /// </summary>
    public partial class OutWeighingSettingUc : System.Windows.Controls.UserControl
    {
        #region variables

        private bool isSetDefaultValue = false;
        #endregion
        public OutWeighingSettingUc()
        {
            InitializeComponent();
            if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)) return;
        }
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            SetDefaultValue();        
        }

        private void SetDefaultValue()
        {
            isSetDefaultValue = true;
            // out   
            this.OutNecessarySendCb.IsChecked = App.Config.WeighingConfig.OutNecessarySend;
            this.OutNecessaryReceiveCb.IsChecked = App.Config.WeighingConfig.OutNecessaryReceive;
            this.OutLockSendCb.IsChecked = App.Config.WeighingConfig.OutBindingSend;
            this.OutCarNumberCb.IsChecked = App.Config.WeighingConfig.OutCheckCarNumber;

            //out times
            if (App.Config.WeighingConfig.OutTimes == 2)
            {
                this.OutTwiceRb.IsChecked = true;
            }
            else if (App.Config.WeighingConfig.OutTimes == 1)
            {
                this.OutOnceRb.IsChecked = true;
            }
            else
            {
                this.OutTwiceRb.IsChecked = true;
            }

            //this.OutNecessaryReceiveYardCb.IsChecked = App.Config.WeighingConfig.OutNecessaryReceiveYard;
            //this.OutNecessarySendYardCb.IsChecked = App.Config.WeighingConfig.OutNecessarySendYard;
            this.OutTranceCompanyCb.IsChecked = App.Config.WeighingConfig.OutNecessaryTranceCompany;

            this.ignoreTraeWeightCb.IsChecked = App.Config.WeighingConfig.OutIgnoreTraeWeighing;
            this.OutCarNumberNeedCb.IsChecked = App.Config.WeighingConfig.OutNecessaryCar;
            this.OutNotSameCompanyCb.IsChecked = App.Config.WeighingConfig.OutNotSameCompany;

            this.OutNecessaryMaterialCb.IsChecked = App.Config.WeighingConfig.OutNecessaryMaterial;

            isSetDefaultValue = false;
        }

        #region OUT 库过磅

        //出库过磅是否绑定发货方为使用单位
        private void OutLockSendCb_Changed(object sender, RoutedEventArgs e)
        {
            if (App.Config.WeighingConfig.OutBindingSend != this.OutLockSendCb.IsChecked)
            {
                App.Config.WeighingConfig.OutBindingSend = (bool)this.OutLockSendCb.IsChecked;
                CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
            }
        }
        //出库过磅 收货方信息是否为必须
        private void OutNecessaryReceiveCb_Changed(object sender, RoutedEventArgs e)
        {
            if (App.Config.WeighingConfig.OutNecessaryReceive != this.OutNecessaryReceiveCb.IsChecked)
            {
                App.Config.WeighingConfig.OutNecessaryReceive = (bool)this.OutNecessaryReceiveCb.IsChecked;
                CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
            }
        }
        //出库过磅 发货方信息是否为必须
        private void OutNecessarySendCb_Changed(object sender, RoutedEventArgs e)
        {
            if (App.Config.WeighingConfig.OutNecessarySend != this.OutNecessarySendCb.IsChecked)
            {
                App.Config.WeighingConfig.OutNecessarySend = (bool)this.OutNecessarySendCb.IsChecked;
                CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
            }
        }
        #endregion

   
        #region 次数设置
        private void OutOnceRb_Checked(object sender, RoutedEventArgs e)
        {         
            if (App.Config.WeighingConfig.OutTimes == 1) return;
            App.Config.WeighingConfig.OutTimes = 1;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
        }

        private void OutTwiceRb_Checked(object sender, RoutedEventArgs e)
        {        
            if (App.Config.WeighingConfig.OutTimes == 2) return;
            App.Config.WeighingConfig.OutTimes = 2;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
        }
        #endregion

        #region 车牌号必须合法

        private void OutCarNumberCb_changed(object sender, RoutedEventArgs e)
        {
            if (App.Config.WeighingConfig.OutCheckCarNumber != this.OutCarNumberCb.IsChecked)
            {
                App.Config.WeighingConfig.OutCheckCarNumber = (bool)this.OutCarNumberCb.IsChecked;
                CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
            }
        }
        #endregion


        #region 发货公司地址必须

        private void OutNecessarySendYardCb_Checked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.WeighingConfig.OutNecessarySendYard = true;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
        }
        private void OutNecessarySendYardCb_Unchecked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.WeighingConfig.OutNecessarySendYard = false;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
        }
        #endregion

        #region 收货公司地址必须


        private void OutNecessaryReceiveYardCb_Checked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.WeighingConfig.OutNecessaryReceiveYard = true;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
        }

        private void OutNecessaryReceiveYardCb_Unchecked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.WeighingConfig.OutNecessaryReceiveYard = false;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
        }
        #endregion


        #region 运输公司地址必须
        private void OutTranceCompanyCb_Checked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.WeighingConfig.OutNecessaryTranceCompany = true;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
        }

        private void OutTranceCompanyCb_Unchecked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.WeighingConfig.OutNecessaryTranceCompany = false;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
        }
        #endregion


        #region 不计皮重

        private void IgnoreTraeWeightCb_Checked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.WeighingConfig.OutIgnoreTraeWeighing = true;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
            DelegeteFactory.Instance.OnWeighingConfigChanged?.Invoke(App.Config.WeighingConfig);
        }

        private void IgnoreTraeWeightCb_Unchecked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.WeighingConfig.OutIgnoreTraeWeighing = false;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
            DelegeteFactory.Instance.OnWeighingConfigChanged?.Invoke(App.Config.WeighingConfig);
        }
        #endregion

        #region 车牌号必须

        private void OutCarNumberNeedCb_Checked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.WeighingConfig.OutNecessaryCar = true;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
        }

        private void OutCarNumberNeedCb_Unchecked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.WeighingConfig.OutNecessaryCar = false;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
        }
        #endregion

        #region 收发公司不能相同
        private void OutNotSameCompanyCb_Checked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.WeighingConfig.OutNotSameCompany = true;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
        }

        private void OutNotSameCompanyCb_Unchecked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.WeighingConfig.OutNotSameCompany = false;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
        }
        #endregion


        #region 货物信息必须
        private void OutMaterialCb_Checked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.WeighingConfig.OutNecessaryMaterial = true;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
        }

        private void OutMaterialCb_Unchecked(object sender, RoutedEventArgs e)
        {
            if (isSetDefaultValue) return;
            App.Config.WeighingConfig.OutNecessaryMaterial = false;
            CommonFunction.UpdateAppConfig(nameof(WeighingConfig));
        }
        #endregion


        /// <summary>
        /// 设置备用字段
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BackupFieldBtn_Click(object sender, RoutedEventArgs e)
        {
            new AddBackupFieldWin()
            {
                BillType = WeightingBillType.Sale,
                Owner = App.Current.MainWindow,
            }.Show();
            
        }
    }
}
